﻿using Mogre;
using ObjCore.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ObjCore.MOgreExt;

namespace ObjCore.Impl
{

    public class Editor : IEditor
    {
        ILoader _l;

        public Editor(ILoader loader)
        {
            _l = loader;
        }

        public void Center()
        {
            var box = _l.CalcBox();

            var cen = box.Center;

            for (int i = 0; i < _l.V.Count; i++)
            {
                var item = _l.V[i];
                item -= cen;
                _l.V[i] = item;
            }
        }

        public void CenterXMin()
        {
            var box = _l.CalcBox();

            var cen = box.Center;
            cen.x = box.Minimum.x;

            for (int i = 0; i < _l.V.Count; i++)
            {
                var item = _l.V[i];
                item -= cen;
                _l.V[i] = item;
            }
        }

        public void CenterYMin()
        {
            var box = _l.CalcBox();

            var cen = box.Center;
            // obj模型的 y z方向 和主流软件相反
            cen.z = box.Minimum.z;

            for (int i = 0; i < _l.V.Count; i++)
            {
                var item = _l.V[i];
                item -= cen;
                _l.V[i] = item;
            }
        }

        public void CenterZMin()
        {
            var box = _l.CalcBox();

            var cen = box.Center;
            // obj模型的 y z方向 和主流软件相反
            cen.y = box.Minimum.y;

            for (int i = 0; i < _l.V.Count; i++)
            {
                var item = _l.V[i];
                item -= cen;
                _l.V[i] = item;
            }
        }

        public void SetSize(double size)
        {
            var box = _l.CalcBox();

            var sx = box.Size.x;
            var sy = box.Size.y;
            var sz = box.Size.z;

            var os = System.Math.Max(sx, sy);
            os = System.Math.Max(os, sz);

            var sclae = (float)(size / os);

            for (int i = 0; i < _l.V.Count; i++)
            {
                var item = _l.V[i];
                item *= sclae;
                _l.V[i] = item;
            }
        }
    }
}
